const moment = require('moment');

    // 转化数字
function transValue(str){
        // 录入值位00-99的字符串
        let first = Number(str[0])
        let second = Number(str[1])
        let number = (first + second)%10
        // 排除特殊情况
        if(str=='10') number=10;
        if(number==0) number=10
        return number
}

    // 转化数字
function transValueNew(str){
        // 录入值位00-99的字符串
        let first = Number(str[0])
        let second = Number(str[1])
        let third = Number(str[2])
        // 第一种情况豹子
        if(first==second&&second==third)return 10;
        if(str=='100')return 10
        let number = (second + third)%10
        // 排除特殊情况
        if(str=='010') number=10;
        if(number==0) number=10
        return number
}
exports.transValue = transValue;

//针对3位数字的倍数转化
function transValue2(str){
        // 录入值位00-99的字符串
        // 取消特殊值020
        if(str=='020') return 0;  //特殊免死位
        let first = Number(str[0])
        let second = Number(str[1])
        let third = Number(str[2])
        if(first==second&&first==third)return 15; //豹子15倍
        if(second==0&&third==0)return 14;//蛮牛
        if(Math.abs(first-second)==1&&Math.abs(second-third)==1&&Math.abs(first-third)==2) return 13; //顺子
        if(first==0&&third==0)return 12; //金牛
        let number = (first + second + third) % 10
        if(number==0) number=10
        return number
}

//针对3位数字的倍数转化  取消020免死规则
function transValue3(str){
        // 录入值位00-99的字符串
        // 取消特殊值020
        // if(str=='020') return 0;  //特殊免死位
        let first = Number(str[0])
        let second = Number(str[1])
        let third = Number(str[2])
        if(first==second&&first==third)return 15; //豹子15倍
        if(second==0&&third==0)return 14;//蛮牛
        if(Math.abs(first-second)==1&&Math.abs(second-third)==1&&Math.abs(first-third)==2) return 13; //顺子
        if(first==0&&third==0)return 12; //金牛
        let number = (first + second + third) % 10
        if(number==0) number=10
        return number
}

    // 解析两位数截图
exports.createData = async (result, task_id, that, name) =>{

        let { ctx } = that;
        let arr = result.split(' ')
        let amount = 0; //计算总金额
        let amounts = arr.filter(item=>item.indexOf('元')>-1).map(item=>{
            amount += (Number(item.slice(-5).slice(0, -1)) *100)
            return { amount: item.slice(-3).slice(0, -1), input: item.slice(-5).slice(0, -1) }
        });
        amount = amount/100;
        //判断如果没有金额，则不做解析操作
        if(amounts.length==0){
            //修改任务为解析失败
            return;
        }
        let date = moment().format('YYYY-MM-DD')
        let time = moment().format('HH:mm:ss')
        let z_index = amounts.length
        let number = amounts.length;
        let z_value = transValue(amounts[z_index-1].amount);
        let z_amount = amounts[z_index-1].amount;
        // 创建详情记录
        let enterDetailList = amounts.map((item,index)=>{
            let result = null;
            let is_z = index+1==z_index?1:0
            let value = transValue(item.amount);
            if(is_z!=1){
                //排除掉本位不计算
                if(z_value>value)result = 1
                if(z_value<value)result = 2
                if(z_value==value)result = 3
            }
            return {
                index: index+1,
                value,
                result,
                is_z,
                amount: item.amount,
                input: item.input
            }
        })
        let type = 1; //默认就是两位
        let win_num = enterDetailList.filter(item=>item.result==1).length
        let los_num = enterDetailList.filter(item=>item.result==2).length
        let win = win_num * z_value;
        let los = 0;
        enterDetailList.forEach(item=>{
            if(item.result==2){
                los +=item.value
            }
        })
        let enterParams = {
            date, time, number, amount, z_value, z_index, z_amount, type, task_id, win, los, win_num, los_num, name
        }
        let t = await ctx.model.transaction();
        try{
            // 创建场次集合
            let enter = await ctx.model.TotalEnter.create(enterParams,{
                transaction: t
            })
            //植入enter_id
            enterDetailList = enterDetailList.map(item=>{
                item.enter_id = enter.id
                return item;
            })
            //添加详细记录
            await ctx.model.TotalEnterDetail.bulkCreate(enterDetailList, {
                transaction: t
            })

            //修改任务状态
            await ctx.model.Task.update({status: 2, enter_id: enter.id}, {
                where: { id:  task_id }
            })

            await t.commit();

        }catch(e){
            console.log(e)
            await t.rollback();
            // 添加失败执行编辑操作
            await ctx.model.Task.update({status: 3},{
                where: { id: task_id }
            })
        }   
}

  // 解析两位数截图
exports.createDataNew = async (result, task_id, that, name) =>{

    let { ctx } = that;
    let arr = result.split(' ')
    let amount = 0; //计算总金额
    let amounts = arr.filter(item=>item.indexOf('元')>-1).map(item=>{
        amount += (Number(item.slice(-5).slice(0, -1)) *100)
        return { amount: item.slice(-5).slice(0, -1).replace('.', ''), input: item.slice(-5).slice(0, -1) }
    });
    amount = amount/100;
    //判断如果没有金额，则不做解析操作
    if(amounts.length==0){
        //修改任务为解析失败
        return;
    }
    let date = moment().format('YYYY-MM-DD')
    let time = moment().format('HH:mm:ss')
    let z_index = amounts.length
    let number = amounts.length;
    let z_value = transValueNew(amounts[z_index-1].amount);
    let z_amount = amounts[z_index-1].amount;
    // 创建详情记录
    let enterDetailList = amounts.map((item,index)=>{
        let result = null;
        let is_z = index+1==z_index?1:0
        let value = transValueNew(item.amount);
        if(is_z!=1){
            //排除掉本位不计算
            if(z_value>value)result = 1
            if(z_value<value)result = 2
            if(z_value==value){
                // 默认为平
                result = 3;
                // 点数相等比大小
                if(Number(z_amount)>Number(item.amount))result = 1
                if(Number(z_amount)<Number(item.amount))result = 2
            }
        }
        return {
            index: index+1,
            value,
            result,
            is_z,
            amount: item.amount,
            input: item.input
        }
    })
    let type = 4; //默认就是两位
    let win_num = enterDetailList.filter(item=>item.result==1).length
    let los_num = enterDetailList.filter(item=>item.result==2).length
    let win = win_num * z_value;
    let los = 0;
    enterDetailList.forEach(item=>{
        if(item.result==2){
            los +=item.value
        }
    })
    let enterParams = {
        date, time, number, amount, z_value, z_index, z_amount, type, task_id, win, los, win_num, los_num, name
    }
    let t = await ctx.model.transaction();
    try{
        // 创建场次集合
        let enter = await ctx.model.TotalEnter3.create(enterParams,{
            transaction: t
        })
        //植入enter_id
        enterDetailList = enterDetailList.map(item=>{
            item.enter_id = enter.id
            return item;
        })
        //添加详细记录
        await ctx.model.TotalEnter3Detail.bulkCreate(enterDetailList, {
            transaction: t
        })

        //修改任务状态
        await ctx.model.Task.update({status: 2, enter_id: enter.id}, {
            where: { id:  task_id }
        })

        await t.commit();

    }catch(e){
        console.log(e)
        await t.rollback();
        // 添加失败执行编辑操作
        await ctx.model.Task.update({status: 3},{
            where: { id: task_id }
        })
    }   
}

// 解析三位数截图
exports.createData2 = async (result, task_id, that, name)=>{
        let { ctx } = that;
        let arr = result.split(' ')
        let amount = 0; //计算总金额
        let amounts = arr.filter(item=>item.indexOf('元')>-1&&item.length>1).map(item=>{
            amount += (Number(item.slice(-5).slice(0, -1)) *100)
            return { amount: item.slice(-5).slice(0, -1).replace('.', ''), input: item.slice(-5).slice(0, -1) }
        })
        amount = amount/100;
        //判断如果没有金额，则不做解析操作
        if(amounts.length==0){
            //修改任务为解析失败
            await ctx.model.Task.update({status: 3},{
                where: { id: task_id }
            })
            return;
        }
        let date = moment().format('YYYY-MM-DD')
        let time = moment().format('HH:mm:ss')
        let z_index = amounts.length
        let number = amounts.length;
        let z_value = transValue2(amounts[z_index-1].amount);
        let z_amount = amounts[z_index-1].amount;
        // 创建详情记录
        let enterDetailList = amounts.map((item,index)=>{
            let result = null;
            let is_z = index+1==z_index?1:0
            let value = transValue2(item.amount);
            if(is_z!=1){
                //排除掉本位不计算
                if(z_value>value)result = 1
                if(z_value<value)result = 2
                if(z_value==value){
                    if(item.amount>z_amount)result = 2;
                    if(item.amount<z_amount)result = 1;
                    if(item.amount==z_amount)result = 1; //同点同金额庄胜
                    if(z_value<=2)result = 1;  //在打平的情况下，值小于等于2则庄赢
                }
                // 特殊值020
                if(z_value==0||value==0)result = 3// 0倍就是020都算平局
            }
            return {
                index: index+1,
                value,
                result,
                is_z,
                amount: item.amount,
                input: item.input
            }
        })
        let type = 2; //三位数
        let win_num = enterDetailList.filter(item=>item.result==1).length
        let los_num = enterDetailList.filter(item=>item.result==2).length
        let win = win_num * z_value;
        let los = 0;
        enterDetailList.forEach(item=>{
            if(item.result==2){
                los +=item.value
            }
        })
        let enterParams = {
            date, time, number, amount, z_value, z_index, z_amount, type, task_id, win, los, win_num, los_num, name
        }
        let t = await ctx.model.transaction();
        try{
            // 创建场次集合
            let enter = await ctx.model.TotalEnter.create(enterParams,{
                transaction: t
            })
            //植入enter_id
            enterDetailList = enterDetailList.map(item=>{
                item.enter_id = enter.id
                return item;
            })
            //添加详细记录
            await ctx.model.TotalEnterDetail.bulkCreate(enterDetailList, {
                transaction: t
            })
            
            //后面5位需要单独记录一个表格
            let enterDetailList5 = enterDetailList.slice(-6)
            enterDetailList5.pop();
            enterDetailList5 = enterDetailList5.reverse().map((item,index)=>{
                item.index = index+1
                return item
            })
            await ctx.model.TotalEnterDetail5.bulkCreate(enterDetailList5, {
                transaction: t
            })
            

            //修改任务状态
            await ctx.model.Task.update({status: 2, enter_id: enter.id}, {
                where: { id:  task_id },
                transaction: t
            })

            //创建的时候，关联创建两个表的数据
            createData3(amounts, enter.id, that)
            await t.commit();

        }catch(e){
            console.log(e)
            await t.rollback();
            // 添加失败执行编辑操作
            await ctx.model.Task.update({status: 3},{
                where: { id: task_id }
            })
        }   
}

// 解析三位数截图 取消020免死规则
// amounts 为解析完成的值 { amount: 153, input: 1.53 }
async function createData3(amounts, enter_id, that){
        let { ctx } = that;
        let amount = 0; //计算总金额
        amounts.forEach(item=>{
            amount += Number(item.input)*100
        })
        amount = amount/100;

        let date = moment().format('YYYY-MM-DD')
        let time = moment().format('HH:mm:ss')
        let z_index = amounts.length
        let number = amounts.length;
        let z_value = transValue3(amounts[z_index-1].amount);
        let z_amount = amounts[z_index-1].amount;
        // 创建详情记录
        let enterDetailList = amounts.map((item,index)=>{
            let result = null;
            let is_z = index+1==z_index?1:0
            let value = transValue3(item.amount);
            if(is_z!=1){
                //排除掉本位不计算
                if(z_value>value)result = 1
                if(z_value<value)result = 2
                if(z_value==value){
                    if(item.amount>z_amount)result = 2;
                    if(item.amount<z_amount)result = 1;
                    if(item.amount==z_amount)result = 1; //同点同金额庄胜
                    if(z_value<=2)result = 1;  //在打平的情况下，值小于等于2则庄赢
                }
                // 特殊值020 - 取消免死规则
                // if(z_value==0||value==0)result = 3// 0倍就是020都算平局
            }
            return {
                index: index+1,
                value,
                result,
                is_z,
                amount: item.amount,
                input: item.input
            }
        })
        let type = 3; //三位数
        let win_num = enterDetailList.filter(item=>item.result==1).length
        let los_num = enterDetailList.filter(item=>item.result==2).length
        let win = win_num * z_value;
        let los = 0;
        enterDetailList.forEach(item=>{
            if(item.result==2){
                los +=item.value
            }
        })
        let enterParams = {
            date, time, number, amount, z_value, z_index, z_amount, type, win, los, win_num, los_num,
            connect_id: enter_id
        }
        let t = await ctx.model.transaction();
        try{
            // 创建场次集合
            let enter = await ctx.model.TotalEnter2.create(enterParams,{
                transaction: t
            })
            //植入enter_id
            enterDetailList = enterDetailList.map(item=>{
                item.enter_id = enter.id
                return item;
            })
            //添加详细记录
            await ctx.model.TotalEnter2Detail.bulkCreate(enterDetailList, {
                transaction: t
            })

            await t.commit();

        }catch(e){
            await t.rollback();
        }   
}

exports.createData3 = createData3;